package com.wnhz.work.yz.fragment.plan;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.google.gson.Gson;
import com.wnhz.work.yz.MyApplication;
import com.wnhz.work.yz.R;
import com.wnhz.work.yz.activity.Plans2Activity;
import com.wnhz.work.yz.adapter.Plan2FragmentAdapter;
import com.wnhz.work.yz.base.BaseFragment;
import com.wnhz.work.yz.bean.PlanFragmentBean;
import com.wnhz.work.yz.utils.Url;
import com.wnhz.work.yz.utils.xutils3.MyCallBack;
import com.wnhz.work.yz.utils.xutils3.XUtil;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by admin on 2017/3/9.
 */
public class Plan2Fragment3 extends BaseFragment {
    //    private List<OrderBean> beanList = new ArrayList<>();

    private View view;
    private Plans2Activity activity;
    private RecyclerView recyclerView;
    private String user_id;

    public void setUser_id(String user_id) {
        this.user_id = user_id;
    }

    public String getUser_id() {
        return user_id;
    }
    @Override
    public void onResume() {
        upLoad();
        super.onResume();
    }
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        view = inflater.inflate(R.layout.fragment_plan_item, container, false);
        activity = (Plans2Activity) getActivity();

        return view;
    }
    private List<PlanFragmentBean.InfoEntity> info = new ArrayList<>();

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        initData();
        initView();
        activity.showDialog();
        upLoad();
    }

    private void upLoad() {
        Map<String, Object> map = new HashMap<>();
        map.put("token", MyApplication.getInstance().getUserBean().getToken());
        map.put("status", 2);   if (user_id != null)
            map.put("user_id", user_id);

        XUtil.Post(Url.PLAN_PLANLIST, map, new MyCallBack<String>() {
            @Override
            public void onSuccess(String result) {
                super.onSuccess(result);
                Log.e("---", result);
                Gson gson = new Gson();
                info.clear();
                PlanFragmentBean bean = gson.fromJson(result, PlanFragmentBean.class);
                info = bean.getInfo();
                planFragmentAdapter.notifyDataSetChanged();
            }

            @Override
            public void onFinished() {
                activity.closeDialog();
                planFragmentAdapter = new Plan2FragmentAdapter(activity, info); planFragmentAdapter.setUser_id(user_id);
                recyclerView.setAdapter(planFragmentAdapter);
                super.onFinished();
            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {
                activity.closeDialog();
                super.onError(ex, isOnCallback);
            }
        });
    }

    private void initData() {

    }

    private Plan2FragmentAdapter planFragmentAdapter;

    private void initView() {
        recyclerView = (RecyclerView) view.findViewById(R.id.recycler);
        recyclerView.setLayoutManager(new LinearLayoutManager(activity, LinearLayoutManager.VERTICAL, false));

    }
}
